package com.coollang.smartbra.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.adapter.LoginPagerAdapter;
import com.coollang.smartbra.fragment.LoginBaseFragment;
import com.coollang.smartbra.utils.ToastUtils;
import com.coollang.smartbra.views.LoginViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by han on 2016/10/25.
 * 登录 注册 忘记密码  公共布局
 */

public class LoginActivity extends MyAutoLayoutActivity {
    @BindView(R.id.view_pager_login)
    public LoginViewPager mViewPager;
    @BindView(R.id.count)
    RelativeLayout mLayout;
    @BindView(R.id.tv_login)
    public TextView mTvLogo;
    private LoginPagerAdapter mAdapter;
    private boolean firstBack = true;//返回两次退出

    @Override
    protected void initView() {
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            // getWindow().setNavigationBarColor(Color.TRANSPARENT);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    @Override
    protected void initData() {
        mAdapter = new LoginPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(1);

        mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {//监听返回键
            switch (mViewPager.getCurrentItem()) {
                case 0: //注册
                    mViewPager.setCurrentItem(1);
                    mTvLogo.setVisibility(View.INVISIBLE);
                    return true;
                case 1: //登录
                    if (firstBack) {
                        ToastUtils.showToast(getString(R.string.go_to_exit));
                        firstBack = false;
                        return true;
                    } else {
                        firstBack = true;
                        return super.onKeyDown(keyCode, event);
                    }
                case 2: //修改密码
                    mViewPager.setCurrentItem(1);
                    mTvLogo.setVisibility(View.INVISIBLE);
                    return true;
                default:
                    return true;
            }
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }


    @OnClick(R.id.tv_login)
    public void onClick() {
        goToLoginView();
    }

    // 去登录页面
    public void goToLoginView() {
        mViewPager.setCurrentItem(1);
        mTvLogo.setVisibility(View.GONE);
    }

    // 去注册页面
    public void goToRegistView() {
        mViewPager.setCurrentItem(0);
        mTvLogo.setVisibility(View.VISIBLE);
    }

    // 去注册页面
    public void goToChangePassView() {
        mViewPager.setCurrentItem(2);
        mTvLogo.setVisibility(View.VISIBLE);
    }

    public LoginBaseFragment getBaseFragment() {
        return mAdapter.getBaseFragment();
    }
}
